#include <iostream>
#include <string>
using namespace std;
class Solution {
    public:
        int romanToInt(string s) {
            int num = 0;
            int i = 0, j;
            int len = s.size();
            while(i<len) {
                j = i + 1;
                if (s[i] == 'V') {
                    num += 5;
                } else if (s[i] == 'L') {
                    num += 50;
                } else if (s[i] == 'D') {
                    num += 500;
                } else if (s[i] == 'M') {
                    num += 1000;
                } else if (s[i] == 'I') {
                    if (j<len) {
                        if (s[j] == 'V') {
                            num += 4;
                            i++;
                        } else if (s[j] == 'X') {
                            num += 9;
                            i++;
                        } else {
                            num += 1;
                        }
                    } else {
                        num += 1;
                    }
                } else if (s[i] == 'X') {
                    if (j<len) {
                        if (s[j] == 'L') {
                            num += 40;
                            i++;
                        } else if (s[j] == 'C') {
                            num += 90;
                            i++;
                        } else {
                            num += 10;
                        }
                    } else {
                        num += 10;
                    }
                } else if (s[i] == 'C') {
                    if (j<len) {
                        if (s[j] == 'D') {
                            num += 400;
                            i++;
                        } else if (s[j] == 'M') {
                            num += 900;
                            i++;
                        } else {
                            num += 100;
                        }
                    } else {
                        num += 100;
                    }
                }
                i++;
            }
            return num;
        }
}
;

int main() {

    Solution s;
    cout << s.romanToInt("III") << endl;
    cout << s.romanToInt("IV") << endl;
    cout << s.romanToInt("IX") << endl;
    cout << s.romanToInt("LVIII") << endl;
    cout << s.romanToInt("MCMXCIV") << endl;

}
